<!DOCTYPE html>
<html>

<head>
	<meta charset="utf-8">
	<meta name="viewport"
		content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0" />
	<meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
	<title>系统设置</title>
	<link rel="stylesheet" type="text/css" href="../css/aui.2.0.css" />
	<style>
		body {}

		p {
			margin-bottom: 15px;
		}
	</style>
</head>

<body>
	<header class="aui-bar aui-bar-nav" style="background-color: #03a9f4">
		<a class="aui-pull-left aui-btn" onclick="closeWin()">
			<span class="aui-iconfont aui-icon-left"></span>
		</a>
		<div class="aui-title" style="padding-top: 10px;">
			<!-- <img src='../image/logo-smaller.png'/> -->
		</div>
	</header>

	<div class="aui-content aui-margin-b-15">
		<ul class="aui-list aui-form-list">
			<li class="aui-list-item">
				<div class="aui-list-item-inner">
					<div class="aui-list-item-label">内网IP</div>
					<div class="aui-list-item-input">
						<input type="text" placeholder="IP地址" id="serverIp" value="192.168.0.65" />
					</div>
				</div>
			</li>
			<li class="aui-list-item">
				<div class="aui-list-item-inner">
					<div class="aui-list-item-label">内网端口号</div>
					<div class="aui-list-item-input">
						<input type="text" placeholder="如8080" id="serverPort" value="8060" />
					</div>
				</div>
			</li>
			<li class="aui-list-item">
				<div class="aui-list-item-inner">
					<div class="aui-list-item-label">内网服务名</div>
					<div class="aui-list-item-input">
						<input type="text" placeholder="服务器访问名称" id="serverProject" value="htsys" />
					</div>
				</div>
			</li>
		</ul>
	</div>

	<div class="aui-content aui-margin-b-15">
		<ul class="aui-list aui-form-list">
			<li class="aui-list-item">
				<div class="aui-list-item-inner">
					<div class="aui-list-item-label">外网IP</div>
					<div class="aui-list-item-input">
						<input type="text" placeholder="IP地址" id="serverIpW" value="106.41.69.46" />
					</div>
				</div>
			</li>
			<li class="aui-list-item">
				<div class="aui-list-item-inner">
					<div class="aui-list-item-label">外网端口号</div>
					<div class="aui-list-item-input">
						<input type="text" placeholder="如8080" id="serverPortW" value="8848" />
					</div>
				</div>
			</li>
			<li class="aui-list-item">
				<div class="aui-list-item-inner">
					<div class="aui-list-item-label">外网服务名</div>
					<div class="aui-list-item-input">
						<input type="text" placeholder="服务器访问名称" id="serverProjectW" value="htsys" />
					</div>
				</div>
			</li>
		</ul>
	</div>

	<!--<div class="aui-content aui-margin-b-15">
			<ul class="aui-list aui-form-list">
				<li class="aui-list-item">
	                <div class="aui-list-item-inner">
	                    <div class="aui-list-item-label">打印机IP</div>
	                    <div class="aui-list-item-input">
	                        <input type="text" placeholder="打印机IP" id="printerIp" value="192.168.1.188"/>
	                    </div>
	                </div>
	            </li>
	            <li class="aui-list-item">
	                <div class="aui-list-item-inner">
	                    <div class="aui-list-item-label">打印机端口</div>
	                    <div class="aui-list-item-input">
	                        <input type="text" placeholder="如6101" id="printerPort" value="6101"/>
	                    </div>
	                </div>
	            </li>
            </ul>
		</div>	-->

	<div class="aui-content aui-margin-b-15">
		<ul class="aui-list aui-form-list">
			<li class="aui-list-item">
				<div class="aui-list-item-inner">
					<div class="aui-list-item-label">网络选择</div>
					<div class="aui-list-item-input">
						<label><input class="aui-radio" type="radio" id="inRadio" name="netRadio" checked value="in"
								onclick="doSelectNet()">内网</label>
						<label><input class="aui-radio" type="radio" id="outRadio" name="netRadio" value="out"
								onclick="doSelectNet()">外网</label>
					</div>
				</div>
			</li>
		</ul>
	</div>

	<div class="aui-content aui-margin-b-15">
		<ul class="aui-list aui-form-list">
			<li class="aui-list-item">
				<div class="aui-list-item-inner">
					<div class="aui-list-item-label">设备唯一号</div>
					<div class="aui-list-item-input">
						<input type="text" placeholder="设备唯一号" id="equipUniqueId" value="DKYZ-01-01" />
					</div>
				</div>
			</li>
		</ul>
	</div>

	<div class="aui-content-padded">
		<p>
		<div class="aui-btn aui-btn-info aui-btn-block" onclick='pingTest()'>网络检测</div>
		</p>
	</div>

	<div class="aui-content-padded">
		<p>
		<div class="aui-btn aui-btn-success aui-btn-block" onclick='doSave()'>保存</div>
		</p>
	</div>

</body>
<script type="text/javascript" src="../script/api.js"></script>
<script type="text/javascript" src="../script/jquery-1.8.2.min.js"></script>
<script type="text/javascript" src="../script/public.js"></script>
<script type="text/javascript">
	apiready = function () {
		var serverIpN = localStorage.getItem('serverIpN');
		var serverPortN = localStorage.getItem('serverPortN');
		var serverProjectN = localStorage.getItem('serverProjectN');
		var serverIpW = localStorage.getItem('serverIpW');
		var serverPortW = localStorage.getItem('serverPortW');
		var serverProjectW = localStorage.getItem('serverProjectW');

		var netChoice = localStorage.getItem('netChoice');

		var equipUniqueId = localStorage.getItem('equipUniqueId');

		if (serverIpN != null || serverPortN != null || serverProjectN != null) {
			$("#serverIp").val(serverIpN);
			$("#serverPort").val(serverPortN);
			$("#serverProject").val(serverProjectN);
		}

		if (serverIpW != null || serverPortW != null || serverProjectW != null) {
			$("#serverIpW").val(serverIpW);
			$("#serverPortW").val(serverPortW);
			$("#serverProjectW").val(serverProjectW);
		}

		if (equipUniqueId != null) {
			$("#equipUniqueId").val(equipUniqueId);
		}

		if (netChoice != null && netChoice == "in") {
			//				//内网
			//				if(serverIp!=null || serverPort!=null || serverProject!=null)
			//				{
			//					$("#serverIp").val(localStorage.getItem('serverIp'));
			//					$("#serverPort").val(localStorage.getItem('serverPort'));
			//					$("#serverProject").val(localStorage.getItem('serverProject'));
			document.getElementById('inRadio').checked = true;
			selectInNet();
			//				}
		} else if (netChoice != null && netChoice == "out") {
			//				//外网
			//				if(serverIp!=null || serverPort!=null || serverProject!=null)
			//				{
			//					$("#serverIpW").val(localStorage.getItem('serverIp'));
			//					$("#serverPortW").val(localStorage.getItem('serverPort'));
			//					$("#serverProjectW").val(localStorage.getItem('serverProject'));
			document.getElementById('outRadio').checked = true;
			selectOutNet();
			//				}
		}
	};
	function closeWin() {
		api.closeWin();
		if (api.pageParam.name != null && api.pageParam.name != "" && api.pageParam.name != undefined) {
			api.execScript({
				name: api.pageParam.name,
				script: 'refresh()'
			});
		}
	}

	function doSelectNet() {
		var obj = document.getElementsByName('netRadio');
		for (var i = 0; i < obj.length; i++) {
			if (obj[i].checked == true) {
				if (obj[i].value == 'in') {
					//选择内网
					selectInNet();
				} else if (obj[i].value == 'out') {
					//选择外网
					selectOutNet();
				}
			}
		}
	}

	function selectInNet() {
		document.getElementById("serverIp").disabled = false;
		document.getElementById("serverPort").disabled = false;
		document.getElementById("serverProject").disabled = false;

		document.getElementById("serverIpW").disabled = true;
		document.getElementById("serverPortW").disabled = true;
		document.getElementById("serverProjectW").disabled = true;
	}

	function selectOutNet() {
		document.getElementById("serverIpW").disabled = false;
		document.getElementById("serverPortW").disabled = false;
		document.getElementById("serverProjectW").disabled = false;

		document.getElementById("serverIp").disabled = true;
		document.getElementById("serverPort").disabled = true;
		document.getElementById("serverProject").disabled = true;
	}

	function pingTest() {
		//获取当前选择网络，并检测网络连接是否成功
		var obj = document.getElementsByName('netRadio');
		var serverIp = "";

		for (var i = 0; i < obj.length; i++) {
			if (obj[i].checked == true) {
				if (obj[i].value == 'in') {
					serverIp = $("#serverIp").val();
				} else if (obj[i].value == 'out') {
					serverIp = $("#serverIpW").val();
				}
			}
		}
		//alert(serverIp);
		console.log("serverIp:" + serverIp);
		var modulePing = api.require('modulePing');
		api.showProgress({
			title: '网络检测中...'
		});
		modulePing.pingtest({
			ip: serverIp
		}, function (ret, err) {
			// alert(JSON.stringify(ret));
			api.hideProgress();
			if (ret) {
				if (ret.okNum > 0) {
					api.toast({
						msg: '网络通畅'
					});
				} else {
					api.toast({
						msg: '网络不通'
					});
				}
			} else {
				alert(JSON.stringify(err));
			}
		});

	}

	function doCheck() {
		//获取当前选择网络，并检测网络连接是否成功
		var obj = document.getElementsByName('netRadio');

		var serverIp = "";
		var serverPort = "";
		var serverProject = "";

		for (var i = 0; i < obj.length; i++) {
			if (obj[i].checked == true) {
				if (obj[i].value == 'in') {
					//选择内网
					serverIp = $("#serverIp").val();
					serverPort = $("#serverPort").val();
					serverProject = $("#serverProject").val();
					if (serverIp != "" && serverPort != "" && serverProject != "") {
						checkNet(serverIp, serverPort, serverProject);
					} else {
						api.toast({
							msg: '请填写完整网络信息',
							duration: 2000,
							location: 'middle'
						});
					}
				} else if (obj[i].value == 'out') {
					//选择外网
					serverIp = $("#serverIpW").val();
					serverPort = $("#serverPortW").val();
					serverProject = $("#serverProjectW").val();
					if (serverIp != "" && serverPort != "" && serverProject != "") {
						checkNet(serverIp, serverPort, serverProject);
					} else {
						api.toast({
							msg: '请填写完整网络信息',
							duration: 2000,
							location: 'middle'
						});
					}
				}
			}
		}
	}

	function checkNet(ip, port, webName) {
		var url = "http://" + ip + ":" + port + "/" + webName + "/autoLoginController!login.m?_u=admin&_p=123456&_pt=min" + "&_to=padController!login.m";
		var params = new Object();
		params.url = url;
		params.method = 'post';
		params.dataType = 'text';
		params.returnAll = 'false';
		params.timeout = 5;

		var data = {
			values: {
				_u: 'admin',
				_p: '123456'
			}
		};

		ajaxForData(params, data, false, function (ret, err) {
			if (ret) {
				//请求成功，表示网络连接正常
				api.toast({
					msg: '网络连接正常',
					duration: 2000,
					location: 'middle'
				});
			} else {

				var errobj = eval(err);
				if (errobj.code == 1) {
					//网络连接超时，提醒切换网络
					api.toast({
						msg: '网络连接超时，请切换网络',
						duration: 2000,
						location: 'middle'
					});
				} else if (errobj.code == 0) {
					//网络连接失败，提醒切换网络
					api.toast({
						msg: '网络连接失败，请切换网络',
						duration: 2000,
						location: 'middle'
					});
				} else {
					api.toast({
						msg: JSON.stringify(err),
						duration: 5000
					});
				}

			}
		});
	}

	function doSave() {

		var serverIpNO = localStorage.getItem('serverIpN');
		var serverPortNO = localStorage.getItem('serverPortN');
		var serverProjectNO = localStorage.getItem('serverProjectN');
		var serverIpWO = localStorage.getItem('serverIpW');
		var serverPortWO = localStorage.getItem('serverPortW');
		var serverProjectWO = localStorage.getItem('serverProjectW');

		var netRadioObj = document.getElementsByName("netRadio");  //这个是以标签的name来取控件
		var netRadioStr = "";

		for (j = 0; j < netRadioObj.length; j++) {
			if (netRadioObj[j].checked) {
				netRadioStr = netRadioObj[j].value;
			}
		}

		var serverIp = "";
		var serverPort = "";
		var serverProject = "";
		var equipUniqueId = "";
		//			var serverUrl = "";

		//			if("in" == netRadioStr)
		//			{
		//				//内网
		//				serverIp = $("#serverIp").val();
		//				serverPort = $("#serverPort").val();
		//				serverProject = $("#serverProject").val();
		//				serverUrl = "http://" + serverIp + ":" + serverPort + "/" + serverProject + "/";
		//			}else{
		//				//外网
		//				serverIp = $("#serverIpW").val();
		//				serverPort = $("#serverPortW").val();
		//				serverProject = $("#serverProjectW").val();
		//				serverUrl = "http://" + serverIp + ":" + serverPort + "/" + serverProject + "/";
		//			}

		localStorage.setItem('netChoice', netRadioStr);
		//保存内网配置信息
		serverIp = $("#serverIp").val();
		serverPort = $("#serverPort").val();
		serverProject = $("#serverProject").val();

		localStorage.setItem('serverIpN', serverIp);
		localStorage.setItem('serverPortN', serverPort);
		localStorage.setItem('serverProjectN', serverProject);
		//保存外网配置信息
		serverIp = $("#serverIpW").val();
		serverPort = $("#serverPortW").val();
		serverProject = $("#serverProjectW").val();

		localStorage.setItem('serverIpW', serverIp);
		localStorage.setItem('serverPortW', serverPort);
		localStorage.setItem('serverProjectW', serverProject);

		//设备唯一号 工位
		equipUniqueId = $("#equipUniqueId").val();
		localStorage.setItem('equipUniqueId', equipUniqueId);

		//			localStorage.setItem('printerIp', $("#printerIp").val());
		//			localStorage.setItem('printerPort', $("#printerPort").val());

		var clearFlag = 'N';
		//判断网络设置是否修改有修改则清空工作中心缓存
		if ($("#serverIp").val() != serverIpNO) {
			clearFlag = 'Y';
		}
		if ($("#serverPort").val() != serverPortNO) {
			clearFlag = 'Y';
		}
		if ($("#serverProject").val() != serverProjectNO) {
			clearFlag = 'Y';
		}
		if ($("#serverIpW").val() != serverIpWO) {
			clearFlag = 'Y';
		}
		if ($("#serverPortW").val() != serverPortWO) {
			clearFlag = 'Y';
		}
		if ($("#serverProjectW").val() != serverProjectWO) {
			clearFlag = 'Y';
		}

		if (clearFlag == 'Y') {
			localStorage.removeItem('workcenterGid');
			localStorage.removeItem('workcenterCode');
			localStorage.removeItem('workcenterName');
		}

		//保存到缓存中的内容，同步写到配置文件ipConfig中
		var ipData = "serverIpN=" + $("#serverIp").val()
			+ ",serverPortN=" + $("#serverPort").val()
			+ ",serverProjectN=" + $("#serverProject").val()
			+ ",serverIpW=" + $("#serverIpW").val()
			+ ",serverPortW=" + $("#serverPortW").val()
			+ ",serverProjectW=" + $("#serverProjectW").val()
			+ ",equipUniqueId=" + $("#equipUniqueId").val();

		console.log("ipData###" + ipData);
		api.writeFile({
			path: 'fs://ipConfig.txt',
			data: ipData
		}, function (ret, err) {
			if (ret.status) {
				console.log("写入数据成功");

				closeWin();
			} else {
				var errMsg = "写入数据失败：" + err.msg + "," + err.code
				console.log(errMsg);
				api.toast({
					msg: errMsg
				});
			}
		});

		//			api.alert({
		//				title: '保存成功',
		//				buttons : ['确定']
		//			}, function(ret, err) {
		//				if (ret.buttonIndex == 1) {
		//					closeWin();
		//				}
		//			});

	}
</script>

</html>